<?php
declare (strict_types=1);

namespace app\platform\controller;

use think\facade\View;
use think\response\Json;

class Role extends PlatformBase
{
    public function lists(): string
    {
        return View::fetch();
    }

    public function list(): Json
    {
        $page         = $this->request->param('page', 1, 'intval');
        $limit        = $this->request->param('limit', 10, 'intval');
        $name         = $this->request->param('name', '', 'trim');
        $status       = $this->request->param('status', 0, 'intval');
        $roleBusiness = new \app\platform\business\Role();

        $where = [];
        if ($name) {
            $where[] = ['name', 'like', '%' . $name . '%'];
        }
        if ($status) {
            $where[] = ['status', '=', $status];
        }

        $roleInfo = $roleBusiness->getRoleList($page, $limit, $where);
        return showList(config(self::_CONFIG_STATUS_SUCCESS_CODE), 'success', $roleInfo['rows'], $roleInfo['count']);
    }

    public function add(): string
    {
        return View::fetch();
    }

    public function doAdd(): Json
    {
        if (!$this->request->isPost()) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_REQUEST_WAY_ERROR_MESSAGE);
        }

        $name   = $this->request->param('name', '', 'trim');
        $status = $this->request->param('status', 0, 'intval');

        $data     = [
            'name'   => $name,
            'status' => $status == self::_IS_ENABLE_STATUS ? self::_IS_ENABLE_STATUS : self::_IS_DISABLE_STATUS,
        ];
        $validate = (new \app\platform\validate\Role())->scene('add');
        if (!$validate->check($data)) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), $validate->getError());
        }

        $roleBusiness = new \app\platform\business\Role();
        $res          = $roleBusiness->addRole($data);
        if ($res[self::_RESULT]) {
            return show(config(self::_CONFIG_STATUS_SUCCESS_CODE), $res[self::_MESSAGE]);
        }

        return show(config(self::_CONFIG_STATUS_ERROR_CODE), $res[self::_MESSAGE]);
    }

    public function edit(): string
    {
        $id = $this->request->param('id', 0, 'intval');
        if (!$id) {
            View::assign(['msg' => self::_PARAMETER_ERROR_MESSAGE]);
            return View::fetch('permission/msg');
        }

        $roleBusiness = new \app\platform\business\Role();
        $role         = $roleBusiness->findRoleById($id);
        if (empty($role)) {
            View::assign(['msg' => self::_PARAMETER_ERROR_MESSAGE]);
            return View::fetch('permission/msg');
        }
        View::assign(["role" => $role]);

        return View::fetch();
    }

    public function doEdit(): Json
    {
        if (!$this->request->isPost()) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_REQUEST_WAY_ERROR_MESSAGE);
        }

        $id     = $this->request->param('id', 0, 'intval');
        $name   = $this->request->param('name', '', 'trim');
        $status = $this->request->param('status', 0, 'intval');

        $data     = [
            'name'   => $name,
            'status' => $status == self::_IS_ENABLE_STATUS ? self::_IS_ENABLE_STATUS : self::_IS_DISABLE_STATUS,
        ];
        $validate = (new \app\platform\validate\Role())->scene('edit');
        if (!$validate->check($data)) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), $validate->getError());
        }

        $roleBusiness = new \app\platform\business\Role();
        $res          = $roleBusiness->updateRole($id, $data);
        if ($res[self::_RESULT]) {
            return show(config(self::_CONFIG_STATUS_SUCCESS_CODE), $res[self::_MESSAGE]);
        }

        return show(config(self::_CONFIG_STATUS_ERROR_CODE), $res[self::_MESSAGE]);
    }

    public function doDelete(): Json
    {
        if (!$this->request->isPost()) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_REQUEST_WAY_ERROR_MESSAGE);
        }

        $id = $this->request->param('id', 0, 'intval');
        if (!$id) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_PARAMETER_ERROR_MESSAGE);
        }

        $roleBusiness = new \app\platform\business\Role();
        $res          = $roleBusiness->deleteRole($id);
        if ($res[self::_RESULT]) {
            return show(config(self::_CONFIG_STATUS_SUCCESS_CODE), $res[self::_MESSAGE]);
        }

        return show(config(self::_CONFIG_STATUS_ERROR_CODE), $res[self::_MESSAGE]);
    }

    public function permission(): string
    {
        $id = $this->request->param('id', 0, 'intval');
        if (!$id) {
            View::assign(['msg' => self::_PARAMETER_ERROR_MESSAGE]);
            return View::fetch('permission/msg');
        }

        $roleBusiness = new \app\platform\business\Role();
        $role         = $roleBusiness->findRoleById($id);
        if (empty($role)) {
            View::assign(['msg' => self::_PARAMETER_ERROR_MESSAGE]);
            return View::fetch('permission/msg');
        }

        View::assign(['role' => $role]);

        $menuBusiness      = new \app\common\business\Menu();
        $menuTree          = $menuBusiness->getRolePermissionMenuTree([$id]);

        View::assign(['menuTree' => json_encode($menuTree, JSON_UNESCAPED_UNICODE)]);

        return View::fetch();
    }

    public function doPermission()
    {
        if (!$this->request->isPost()) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_REQUEST_WAY_ERROR_MESSAGE);
        }

        $roleId = $this->request->param('id', 0, 'intval');

        $rows   = [];
        $params = $this->request->param();
        foreach ($params as $key => $value) {
            if (!str_starts_with($key, 'layuiTreeCheck')) {
                continue;
            }

            $rows [] = ['role_id' => $roleId, 'menu_id' => intval($value)];
        }

        $roleBusiness = new \app\platform\business\Role();
        $res          = $roleBusiness->updateRoleMenuRelation($roleId, $rows);
        if ($res[self::_RESULT]) {
            return show(config(self::_CONFIG_STATUS_SUCCESS_CODE), $res[self::_MESSAGE]);
        }

        return show(config(self::_CONFIG_STATUS_ERROR_CODE), $res[self::_MESSAGE]);
    }
}
